/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.user.shared;

import java.util.ArrayList;
import java.util.List;

/**
 * This class provides methods for manipulating {@link Link}s.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class Links {

  public static void addSubLink(Link link, Link subLink) {
    if (link.getSubLinks() == null) {
      link.setSubLinks(new ArrayList<Link>());
    }
    link.getSubLinks().add(subLink);
  }

  /**
   * Retrieves the default leaf of link.
   * 
   * @return the default leaf of link.
   */
  public static Link getDeafultLeaf(Link node) {
    if (node.getDisabled()) {
      return null;
    } else if (node.getValue() != null) {
      return node;
    } else if (node.getSubLinks() != null) {
      for (Link sub : node.getSubLinks()) {
        Link leaf = Links.getDeafultLeaf(sub);
        if (leaf != null) {
          return leaf;
        }
      }
    }
    return null;
  }

  /**
   * Retrieves the default leaf of link.
   * 
   * @return the default leaf of link.
   */
  public static Link getDeafultLeaf(List<Link> links) {
    for (Link link : links) {
      Link leaf;
      if ((leaf = Links.getDeafultLeaf(link)) != null) {
        return leaf;
      }
    }
    // null object
    return new Link("", null);
  }

  /**
   * Retrieves leaf link from the given link.
   * 
   * @param link the link
   * @param linkValue the link value
   * @return the leaf link
   */
  public static Link getLeaf(Link link, String linkValue) {
    if (link.getValue() != null) {
      if (link.getValue().equals(linkValue)) {
        return link;
      }
      return null;
    } else if (link.getSubLinks() != null) {
      return Links.getLeaf(link.getSubLinks(), linkValue);
    } else {
      return null;
    }
  }

  /**
   * Retrieves leaf link from the given links.
   * 
   * @param links the links
   * @param linkValue the link value
   * @return the leaf link
   */
  public static Link getLeaf(List<Link> links, String linkValue) {
    for (Link link : links) {
      Link leaf = Links.getLeaf(link, linkValue);
      if (leaf != null) {
        return leaf;
      }
    }
    return null;
  }

  /**
   * Finds nodes from root to leaf form the given link with the specified leaf value.
   * 
   * @param link the link
   * @param leafValue the leaf value to link
   * @return the linked nodes from root to leaf, if no leaf link matches to the specified leaf
   *         value, returns null
   */
  public static List<Link> getLinkedNodes(Link link, String leafValue) {
    if (leafValue == null) {
      return null;
    }
    if (link.getDisabled()) {
      return null;
    } else if (link.getValue() != null) {
      if (link.getValue().equals(leafValue)) {
        List<Link> links = new ArrayList<Link>();
        links.add(link);
        return links;
      }
    } else {
      for (Link sub : link.getSubLinks()) {
        List<Link> links = Links.getLinkedNodes(sub, leafValue);
        if (links != null) {
          links.add(0, link);
          return links;
        }
      }
    }
    return null;
  }

  /**
   * Finds nodes from root to leaf form the given links with the specified leaf value.
   * 
   * @param link the link
   * @param leafValue the leaf value to link
   * @return the linked nodes from root to leaf, if no leaf link matches to the specified leaf
   *         value, returns null
   */
  public static List<Link> getLinkedNodes(List<Link> links, String leafValue) {
    if (leafValue == null) {
      return null;
    }
    for (Link link : links) {
      List<Link> nodes = Links.getLinkedNodes(link, leafValue);
      if (nodes != null) {
        return nodes;
      }
    }
    return null;
  }

  /**
   * Retrieves the root link from given links.
   * 
   * @param links the links
   * @param linkValue the value of a leaf {@link Link} over the links.
   * @return the root link
   */
  public static Link getRoot(List<Link> links, String linkValue) {
    for (Link link : links) {
      if (Links.getLeaf(link, linkValue) != null) {
        return link;
      }
    }
    return null;
  }

  /**
   * Links each link's value to leaf's.
   * 
   * @param values the {@link Link} values
   * @return the {@link Link}s each with value of leaf
   * @see #linkToLeaf(Link)
   */
  public static List<Link> linkToLeaves(List<Link> links) {
    if (links == null) {
      return null;
    }
    List<Link> options = new ArrayList<Link>();
    for (Link link : links) {
      options.add(linkToLeaf(link));
    }
    return options;
  }

  /**
   * Links the given link's value to its leaf's.
   * 
   * @param values the {@link Lin}
   * @return the {@link Link} with value of leaf
   */
  public static Link linkToLeaf(Link link) {
    Link option = new Link();
    Link leaf = Links.getDeafultLeaf(link);
    if (leaf == null) {
      throw new IllegalArgumentException(
          "No any available leaf link was found. Did you forget to give your link a value?");
    }
    option.setName(link.getName());
    // leaf value
    option.setValue(leaf.getValue());
    // option.setDisabled(link.getDisabled());
    return option;
  }

}
